package main;

import java.io.IOException;
import java.util.regex.*;

public class TextProcessor {
	
	String line,sign="";
	
	public TextProcessor(String line) {
		this.line=line;
	}
	
	public static void RemoveSpaces() {
		// Remove not informative spaces and tabs
		Sjavac.line=Sjavac.line.replaceAll("\\s+", " ");
		Sjavac.line=Sjavac.line.trim();
	}
	
	public static void removeSpecialSpaces () {
		Pattern pattern = Pattern.compile("(.*?)( ?([=;\\(\\)\\{\\},]) ?)");
		Matcher matcher = pattern.matcher(Sjavac.line);
		boolean isMattch=matcher.find();
		while (isMattch) {
			Sjavac.line=matcher.replaceAll("$1$3");
			isMattch=matcher.find();
		}
		
	}
	public static void handleMultiComment(String line) throws IOException, compilationException {
		
		
	}
	public static void removeComments () throws IOException, compilationException {
		Pattern pattern = Pattern.compile("(.*?)(//)(.*)");
		Matcher matcher = pattern.matcher(Sjavac.line);
		if(matcher.find()) {
			Sjavac.line=matcher.group(1);
			return;
		}
		
		pattern = Pattern.compile("(.*?)(/\\*)(.*)");
		Matcher OpenMatcher = pattern.matcher(Sjavac.line);
		if(OpenMatcher.find()) {
			String before=OpenMatcher.group(1);
			boolean isCloesd=false;
			
			while (Sjavac.line!=null) {
				pattern = Pattern.compile("(.*?)(\\*/)(.*)");
				Matcher CloseMatcher = pattern.matcher(Sjavac.line);
				if (CloseMatcher.find()) {
					isCloesd=true;
					Sjavac.line=before+CloseMatcher.group(3);
					removeComments();
					break;
				}
				before=null;
				Sjavac.line=Sjavac.nextLine();
			}
			if (!isCloesd)
				throw new compilationException("Error 6: Uncloesd multi lines comments");
			Sjavac.nextLine();
		}
	}
	
	public String findNextSeparatedBy (String separator) throws compilationException {
		Pattern pattern = Pattern.compile(".+?"+separator);
		Matcher matcher = pattern.matcher(this.line);
		if (matcher.find()) {
			String found=this.line.substring(matcher.start(), matcher.end()-1);
			this.sign=String.valueOf(line.charAt(matcher.end()-1));
			this.line=line.substring(matcher.end(), line.length());
			return found;
		}
		throw new compilationException("Error 12: UnKnown command");
	}
	public String findFinalSeparatedBy (String separator) throws compilationException {
		String found=this.line.substring(0, this.line.lastIndexOf(separator));
		this.sign=separator;
		this.line=null;
		return found;
		//throw new compilationException("Error 12: UnKnown command");
	}
	
	public String findNextWord () {
		try {
			return findNextSeparatedBy ("[ =;\\(\\)\\{\\},]");
		} catch (Exception e) {
			return getLine();
		}
	}
	public String getSeparatedSign () {
		return this.sign;
	}
	public String getLine () {
		if (this.line==null || this.line.isEmpty())
			return null;
		String line=this.line;
		this.line=null;
		return line;
	}
	public int indexOf (String subString) {
		return this.line.indexOf(subString);
	}
	public static String removeLast (String line) {
		return line.substring(0, line.length()-1);
	}
	public static void cleanCode () throws IOException, compilationException {
		TextProcessor.RemoveSpaces();
		TextProcessor.removeSpecialSpaces();
		TextProcessor.removeComments();
		
		if (Sjavac.line.isEmpty()) {
			Sjavac.nextLine();
			cleanCode();
		}
	}
	
}
